package com.las.bot.cmd.bili;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.las.annotation.BotCmd;
import com.las.bot.cmd.qa.QaAnswerCmd;
import com.las.bot.dao.BiliLiveDao;
import com.las.cmd.BaseNonCommand;
import com.model.BiliLive;
import com.utils.BotUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@BotCmd(funName = "B站功能",isMatch = false)
public class BiliCmd extends BaseNonCommand {

    private static Logger logger = Logger.getLogger(BiliCmd.class);

    @Autowired
    private BiliLiveDao biliLiveDao;

    @Override
    public void execute(JSONObject obj,Long userId, Long id, Integer type, String command, ArrayList<String> args) throws Exception {
        if(command.contains("live.bilibili.com")){
            List<BiliLive> lives = biliLiveDao.findAll();
            logger.info("开始匹配所有直播间：" + JSONObject.toJSONString(lives));
            for (BiliLive life : lives) {
                String liveSite = life.getLiveSite();
                logger.info("检查内容是否有B站直播间：" + liveSite);
                if(command.contains(liveSite)){
                    logger.info("匹配成功");
                    String descStr = life.getDescStr();
                    String title = life.getTitle();
                    String jsonStr = life.getContent();
                    String str = jsonStr.replaceAll("TEMP_TITLE", title).replaceAll("TEMP_DESC", descStr);

                    List<JSONObject> list = Lists.newArrayList();
                    JSONObject object = new JSONObject();
                    object.put("type", "App");
                    object.put("content", str);
                    list.add(object);
                    BotUtils.sendCusMsg(userId, id, type, list);
                }
            }
        }
    }


}
